import json

from cjc_trackers.tracker_base import *

memory_helper = MemoryShareHelper()


def main(option):
    global memory_helper
    check_requirements(requirements=ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
    tracker = Tracker(option)
    tracker.run_wrapper()


def processor_run_tracker(track_queue, can_track):
    """ps 是 pipe生产的管道列表 """
    _opt = sort_yolov5_opts.create_opt(
        yolo_weights=WEIGHTS / 'yolov5s.pt'
        , strong_sort_weights=WEIGHTS / 'osnet_x0_25_msmt17.pt'
        , config_strongsort=ROOT / 'strong_sort/configs/strong_sort.yaml'
        , project=ROOT / 'runs/track',
        source=project_setting.camera_index
    )
    _opt.hide_labels = False
    _opt.hide_conf = True
    _opt.hide_class = True
    _opt.show_vid = project_setting.show_track_window  # 不显示
    logging.disable()
    tracker = Tracker(_opt, can_track)
    tracker.set_conn(track_queue)
    # tracker.set_video_finished_cb(on_record_finished)  #不用单独传了
    try:
        tracker.run_wrapper()
    except KeyboardInterrupt as e:
        print("手工退出", e)


def on_record_finished(f, status):
    # 传递给websocket
    memory_helper.add_text_to_list(f)
    memory_helper.add_text_to_list(json.dumps(status))
    memory_helper.list_to_memory()
    # memory_helper.add_text_to_list(status)


if __name__ == "__main__":
    # opt = parse_opt()
    opt = sort_yolov5_opts.create_opt(
        yolo_weights=WEIGHTS / 'yolov5m.pt',
        strong_sort_weights=WEIGHTS / 'osnet_x0_25_msmt17.pt',
        config_strongsort=ROOT / 'strong_sort/configs/strong_sort.yaml',
        project=ROOT / 'runs/track',
        source="C:/Users/cjc.win/Desktop/bsb.mp4"
    )
    print("opt  ", opt)
    opt.save_crop = True
    opt.save_txt = True
    logging.disable()
    # opt.show_vid = True
    main(opt)
